//
//  BridgeClass.m
//  prova
//
//  Created by Gianluca Puglia on 27/03/13.
//  Copyright (c) 2013 Gianluca Puglia. All rights reserved.
//

#import "BridgeClass.h"
#import "ResultData.h"
#import <Python/Python.h>

@implementation BridgeClass

+ (BridgeClass *)sharedInstance {
    static dispatch_once_t pred;
    static BridgeClass *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[BridgeClass alloc] init];
    });
    return shared;
}

- (id)init {
	if ((self = [super init])) {
        Class PythonStuffClass = NSClassFromString(@"PythonStuff");
        myPythonStuff = [PythonStuffClass new];
	}
	return self;
}

- (void)startWithPath:(NSString *)path onlyConversion:(BOOL)onlyConversion {
    NSDictionary *d = [[NSDictionary alloc] initWithObjectsAndKeys:path, @"path", [NSNumber numberWithBool:onlyConversion], @"conversion", nil];
    PyEval_InitThreads();
    [NSThread detachNewThreadSelector:@selector(pyStartWithDict:) toTarget:myPythonStuff withObject:d];
    PyEval_ReleaseLock();
}

- (void)finalizeWithAlbums:(NSArray *)albums onCopy:(NSString *)copyOrMove format:(NSString *)format destination:(NSString *)destination {
    NSDictionary *d = [[NSDictionary alloc] initWithObjectsAndKeys:albums, @"albums", copyOrMove, @"copy", format, @"format", destination, @"dest", nil];
    PyEval_InitThreads();
    [NSThread detachNewThreadSelector:@selector(pyFinalizeWithDict:) toTarget:myPythonStuff withObject:d];
    PyEval_ReleaseLock();
}

+ (void)updateProgressBar:(NSDictionary *)dict {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProgressBar" object:dict];
}

+ (void)updateStatusBar:(NSDictionary *)dict {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatusBar" object:dict];
}

+ (void)finishedWithResults:(NSArray *)results {
    ResultData *rd = [[ResultData alloc] initWithAlbums:results];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"finished" object:rd];
}

+ (void)finalizeFinished {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"finalizeFinished" object:nil];
}

@end
